﻿@page "/TreeTest"
@using Masuit.Tools.Models
@inject Radzen.DialogService DialogService

<h3>TreeTest</h3>
<Antdesign.Card>
    <AntDesign.Tree Selectable IconExpression="@(x => x.DataItem.Children.Count>0?"folder-open":"file")" TItem="Data" ShowLine="false"
                    ShowIcon="true" OnSelect="@SelectedDataChanged" SelectedKey="@searchKey" DefaultSelectedKey="@searchKey"
                    ShowLeafIcon="true" KeyExpression="x => x.DataItem.Id"
                    DataSource="datas" TitleExpression="x => x.DataItem.Title"
                    ChildrenExpression="x => x.DataItem.Children">

    </AntDesign.Tree>
</Antdesign.Card>

<RadzenButton Text="Refresh" Click="RefreshTreeClick"></RadzenButton>
<RadzenButton Text="GetValue" Click="BtnGetValueClick"></RadzenButton>

<RadzenButton Text="Confirm dialog" ButtonStyle="Radzen.ButtonStyle.Secondary"
              Click=@ConfirmClick />

@code {
    public string searchKey = "0-0-1-1";

    public void SelectedDataChanged(AntDesign.TreeEventArgs<Data> data)
    {
        searchKey = data.Node.DataItem.Id;
    }
    public void BtnGetValueClick(MouseEventArgs args)
    {

    }

    public void RefreshTreeClick(MouseEventArgs args)
    {
        List<Data> list = new List<Data>();
        list.Add(new Data
            {
                Id = "0-0",
                Title = "0-0",
                ParentId = null,
            });
        list.Add(new Data
            {
                Id = "0-0-0",
                Title = "0-0-0",
                ParentId = "0-0",
            });
        list.Add(new Data
            {
                Id = "0-0-1",
                Title = "0-0-1",
                ParentId = "0-0",
            });

        list.Add(new Data
            {
                Id = "0-0-1-0",
                Title = "0-0-1-0",
                ParentId = "0-0-1",
            });
        list.Add(new Data
            {
                Id = "0-0-1-1",
                Title = "0-0-1-1",
                ParentId = "0-0-1",
            });
        list.Add(new Data
            {
                Id = "0-0-1-2",
                Title = "0-0-1-2",
                ParentId = "0-0-1",
            });

        list.Add(new Data
            {
                Id = "0-1",
                Title = "0-1",
                ParentId = null,
            });
        list.Add(new Data
            {
                Id = "0-1-0",
                Title = "0-1-0",
                ParentId = "0-1",
            });
        list.Add(new Data
            {
                Id = "0-1-1",
                Title = "0-1-1",
                ParentId = "0-1",
            });
        //
        list.Add(new Data
            {
                Id = "0-1-0-0",
                Title = "0-1-0-0",
                ParentId = "0-1-0",
            });

        list.Add(new Data
            {
                Id = "0-2",
                Title = "0-2",
                ParentId = null,
            });

        list.Add(new Data
            {
                Id = "0-3",
                Title = "0-31",
                ParentId = null,
            });

        list.Add(new Data
            {
                Id = "0-4",
                Title = "0-4",
                ParentId = null,
            });
        list.Add(new Data
            {
                Id = "0-5",
                Title = "0-5",
                ParentId = null,
            });
        list.Add(new Data
            {
                Id = "0-6",
                Title = "0-6",
                ParentId = null,
            });
        datas = list.ToTree();
    }

    public async void ConfirmClick(MouseEventArgs args)
    {
        var flag = await DialogService.Confirm("Are you sure?", options: new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No",  });
        if (flag == null || false == flag.Value)
        {

        }
        else
        {

        }
    }

    public List<Data> datas = new List<Data>();
    protected override async Task OnInitializedAsync()
    {
        base.OnInitializedAsync();
    }

    public class Data : ITreeEntity<Data>
    {
        // public Data(string Id, string Title)
        // {
        //     this.Id = Id;
        //     this.Title = Title;
        // }

        public string Id { get; set; }

        public string Title { get; set; }

        public ICollection<Data> Children { get; set; }

        public string ParentId { get; set; }
    }
}
